package com.shizhanzhe.zxbapp.task;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.APP2;
import com.shizhanzhe.zxbapp.bean.CustomObject;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.utils.Util;

import java.util.List;

import xcs.ert.jlj.os.df.AdExtraTaskStatus;
import xcs.ert.jlj.os.df.AppExtraTaskObject;
import xcs.ert.jlj.os.df.AppExtraTaskObjectList;
import xcs.ert.jlj.os.df.AppSummaryObject;

/**
 * Created by zz9527 on 2018/10/15.
 */
public class LoadingAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context mContext;
    private List<CustomObject> mCustomObjectList;

    public LoadingAdapter(List<CustomObject> mCustomObjectList, Context context) {
        this.mCustomObjectList = mCustomObjectList;
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mCustomObjectList.size();
    }

    @Override
    public Object getItem(int i) {
        return mCustomObjectList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("CheckResult")
    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_lv_zhengzaijinxing, null);
            holder = new ViewHolder();
            holder.app_icon = convertView.findViewById(R.id.zj_iv);
            holder.app_name = convertView.findViewById(R.id.zj_title);
            holder.app_points = convertView.findViewById(R.id.zj_jf);
            holder.app_detail = convertView.findViewById(R.id.zj_detail);
            holder.app_status = convertView.findViewById(R.id.zj_status);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        CustomObject item = mCustomObjectList.get(i);
        final AppSummaryObject appSummaryObject = item.getAppSummaryObject();
        // 设置广告图标
        if (item.getAppicon() == null) {
            holder.app_icon.setVisibility(View.INVISIBLE);
        } else {
            Glide.with(mContext).load(item.getAppicon()).into(holder.app_icon);
            holder.app_icon.setVisibility(View.VISIBLE);
        }
        // 设置广告名字
        if (appSummaryObject.getAppName().contains("#")) {
            holder.app_name.setText(appSummaryObject.getAppName().split("#")[0]);
        } else {
            holder.app_name.setText(appSummaryObject.getAppName());
        }
        // 设置广告积分
//        holder.app_points.setText((double) appSummaryObject.getPoints() / 1000 + "元");
//        holder.app_detail.setText(item.getAppSummaryObject().getAdSlogan());
        // 如果是同一个广告需要展示多次的话，就标识这个item是专门请求追加任务的item，需要特殊处理
        if (item.isShowMultSameAd()) {
            AppExtraTaskObjectList extraTaskList =
                    item.getAppSummaryObject().getExtraTaskList();
            if (extraTaskList != null && extraTaskList.size() > 0) {
                AppExtraTaskObject extraTaskObject = extraTaskList.get(item.getShowExtraTaskIndex());
                if (extraTaskObject.getStatus() == AdExtraTaskStatus.NOT_START) {
                    holder.app_status.setText("任务等待中");
                    holder.app_status.setTextColor(mContext.getResources().getColor(R.color.blue));
                    holder.app_points.setText("¥" + Util.Sub((double) extraTaskObject.getPoints() * Double.parseDouble(UserBean.divide) / 100 + ""));
                    holder.app_points.setTextColor(mContext.getResources().getColor(R.color.red));
                } else if (extraTaskObject.getStatus() == AdExtraTaskStatus.IN_PROGRESS) {
                    holder.app_status.setText("任务进行中");
                    holder.app_status.setTextColor(mContext.getResources().getColor(R.color.them));
                    holder.app_points.setText("¥" + Util.Sub((double) extraTaskObject.getPoints() * Double.parseDouble(UserBean.divide) / 100 + ""));
                    holder.app_points.setTextColor(mContext.getResources().getColor(R.color.red));
                }
                holder.app_detail.setText(extraTaskObject.getAdText());
            }
        }

        // 到这里就标识是其他广告列表请求
        else {
                holder.app_status.setText("任务进行中");
                holder.app_status.setTextColor(mContext.getResources().getColor(R.color.them));

            holder.app_points.setText("¥" + appSummaryObject.getAppName().split("#")[3] );
            holder.app_points.setTextColor(mContext.getResources().getColor(R.color.red));
            holder.app_detail.setText("完成任务获得奖励");

            Glide.with(mContext).load(item.getAppicon()).into(holder.app_icon);
            holder.app_icon.setVisibility(View.VISIBLE);
//            AppExtraTaskObjectList extraTaskList =
//                    item.getAppSummaryObject().getExtraTaskList();
//
//            if (extraTaskList != null && extraTaskList.size() > 0) {
//
//                for (int k = 0; k < extraTaskList.size(); ++i) {
//                    AppExtraTaskObject extraTaskObject = extraTaskList.get(k);
//                    if (extraTaskObject.getStatus() == AdExtraTaskStatus.NOT_START ||
//                            extraTaskObject.getStatus() == AdExtraTaskStatus.IN_PROGRESS) {
//                        holder.app_status.setText("追加奖励");
//                        holder.app_points.setText("+" + (double)appSummaryObject.getPoints()/1000+"元");
//                        holder.app_detail.setText(extraTaskObject.getAdText());
//                        break;
//                    }
//                }
//            }
        }

        return convertView;
    }

    private static class ViewHolder {
        ImageView app_icon;

        TextView app_name;

        TextView app_points;

        TextView app_detail;

        TextView app_status;
    }
}
